package HashTable;

import java.util.*;

public class _720_LongestWordinDictionary {
    public String longestWord(String[] words) {
        Arrays.sort(words);
        Set<String> set = new HashSet<>();
        set.add("");
        String longestword = null;
        int length = Integer.MIN_VALUE;
        for (String word : words) {
            if (set.contains(word.substring(0, word.length() - 1))) {
                set.add(word);
            }
        }
        for (String word : words) {
            if (set.contains(word)) {
                int templength = word.length();
                if (templength > length) {
                    longestword = word;
                    length = templength;
                }
            }
        }
        return longestword;
    }
}
